package com.test.application.designPatterns.singleton.demo7;

import java.io.Serializable;

/**
 * 描述: 破坏单例模式: 方式一: 从文件读取
 * 作者: yanruizhi
 * 时间: 2024/9/18 17:42
 */
public class Singleton implements Serializable {
    private Singleton() {
    }

    private static class SingletonHolder {
        private static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

    // 如果添加此方法, 则反序列化时, 会返回同一个对象,解决了单例模式被破坏的问题
    private Object readResolve() {
        return SingletonHolder.INSTANCE;
    }
}
